package com.example.studentadminsystem

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.google.gson.Gson
import edu.njfu.networkdemo.adapter.StudentAdapter
import edu.njfu.networkdemo.model.Student
import edu.njfu.networkdemo.service.StudentService
import edu.njfu.networkdemo.utils.ServerCreator
import kotlinx.android.synthetic.main.activity_second.*

class SecondActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        var students:ArrayList<Student>;
        var students1:List<Student>?;

        val studentService= ServerCreator.create<StudentService>();
        studentService.getAllStudentData().enqueue(object: retrofit2.Callback<List<Student>> {
            override fun onFailure(call: retrofit2.Call<List<Student>>, t: Throwable) {
                TODO("Not yet implemented")
            }

            override fun onResponse(
                call: retrofit2.Call<List<Student>>,
                response: retrofit2.Response<List<Student>>
            ) {
                students1=response.body()
                if(students1!=null){
                    var adapter=StudentAdapter(this@SecondActivity,R.layout.student_info_layout,students1!!)
                    studentList.adapter=adapter
                    studentList.setOnItemClickListener{
                            parent, view, position, id ->
                        val student= students1!![position]
                        var studentInfo= Gson().toJson(student);
                         var intent= Intent(this@SecondActivity,ThirdActivity::class.java)
                        intent.putExtra("studentInformation",studentInfo)
                        startActivity(intent)
                }
                }
            }
        })
    }
}
